package sougou2google;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
    /**
     * 名称:sougou2google
     * 用途:将搜狗拼音细胞词库(TXT格式)转为谷歌拼音可用的词库<br>
     * 作者：加州旅客<br>
     * E-mail:jiazhoulvke@gmail.com<br>
     * 说明:这个小程序里用到的lib来自一个开源项目pinyin4j,网址为<a href="http://sourceforge.net/projects/pinyin4j/">http://sourceforge.net/projects/pinyin4j/</a><br>
     */
public class Main {
    
     /**
     * 获取一个字符的拼音
     */
    public static String getCharPinyin(char c) {
        HanyuPinyinOutputFormat hanyuPinyinFormat = new HanyuPinyinOutputFormat(); // 定义拼音的格式，详细用法请看其说明文档
        hanyuPinyinFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        hanyuPinyinFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String[] pinyinArray = null;
        try {
            pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c,
                    hanyuPinyinFormat);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        if (pinyinArray != null) {
            return pinyinArray[0];
        }
        return "";
    }
    
    /**
     * 获取一个字符串的拼音
     */
    public static String getStrPinyin(String str) {

        char[] chars = str.trim().toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            String pinyin = getCharPinyin(chars[i]);
            if (pinyin.equals("")) {
                return "";
            }
            sb.append(pinyin + " ");
        }
        return sb.toString().trim();
    }

    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("名称:sougou2google");
            System.out.println("用途:将搜狗拼音细胞词库(TXT格式)转为谷歌拼音可用的词库");
            System.out.println("E-mail:jiazhoulvke@gmail.com");
            System.out.println("使用方法:java -jar sogou2google.jar 搜狗词库.txt 谷歌词库.txt\n");
            return;
        }
        String fi = args[0];
        String fo = args[1];
        int errorCount = 0; // 无法转化的词的个数
        ArrayList errorWord = new ArrayList();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(fi));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("搜狗词库未找到!");
        }
        System.out.println("开始转换，如果词库过大，将需要多花费一些时间，请稍等……\n");
        BufferedReader br = new BufferedReader(new InputStreamReader(
                fileInputStream));
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(fo));
            String str = null;
            while ((str = br.readLine()) != null) {
                if (!getStrPinyin(str).equals("")) {
                    bw.write(str + " 1 " + getStrPinyin(str) + "\n");
                } else {
                    errorWord.add(str);
                    errorCount++;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
                br.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("转换完成！谢谢你的使用！\n");
        if (errorCount > 0) {
            System.out.println("有" + errorCount + "个生僻词无法识别！");
            System.out.println("无法转换的词为:");
            for (int i = 0; i < errorWord.size(); i++) {
                System.out.println((i+1) + ":" + errorWord.get(i));
            }
            System.out.println();
        }
    }
}
